package net.tmaize.lockview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import net.tmaize.lockview.bean.Point;
import net.tmaize.lockview.listrner.InputFinshListrner;
import java.util.ArrayList;
import java.util.List;

public class LockView extends View {

    private String input = null;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float radius = 20;
    private Point[][] points = null;
    private List<Point> points_hovered = new ArrayList<>();
    private boolean action_up;
    private InputFinshListrner inputFinshListrner;
    float moveX;
    float moveY;

    public LockView(Context context) {
        super(context);
    }

    public LockView(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    public LockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        paint.setColor(Color.BLACK);
        //设置线的粗细
        paint.setStrokeWidth(10);

        float x_width = canvas.getWidth() / 6;
        float y_height = canvas.getHeight() / 6;

        if (points==null){
            points = new Point[3][3];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    points[i][j] = new Point((2 * j + 1) * x_width,(2 * i + 1) * y_height,3*i+j+1);
                    canvas.drawCircle(points[i][j].getX(), points[i][j].getY(), radius, paint);
                }
            }
        }else{
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    canvas.drawCircle(points[i][j].getX(), points[i][j].getY(), radius, paint);
                }
            }
        }


        int size = points_hovered.size();

        //两点之间有线可以连接
        if (size>=2){
            for (int i=0;i<size-1;i++){
                canvas.drawLine(points_hovered.get(i).getX(), points_hovered.get(i).getY(),points_hovered.get(i+1).getX(), points_hovered.get(i+1).getY(),paint);
            }
        }

        //跟着手指的一条线
        if (size>=1){
            if (!action_up){
                canvas.drawLine(points_hovered.get(size-1).getX(), points_hovered.get(size-1).getY(),moveX,moveY,paint);
            }

        }

        //最后一个圆点要突出,同时最后抬起来时最后一个圆的要缩回去
        if (size>=1) {
            if (action_up){
                canvas.drawCircle(points_hovered.get(size-1).getX(), points_hovered.get(size-1).getY(), radius, paint);
            }else{
                canvas.drawCircle(points_hovered.get(size-1).getX(), points_hovered.get(size-1).getY(), radius+10, paint);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        action_up = false;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://按下的状态

                moveX = event.getX();
                moveY = event.getY();
                addToHovered(Point.isOnPoint(event.getX(),event.getY(), 3*radius,points));
                break;
            case MotionEvent.ACTION_MOVE://移动状态
                moveX = event.getX();
                moveY = event.getY();
                addToHovered(Point.isOnPoint(event.getX(),event.getY(), 3*radius,points));
                break;
            case MotionEvent.ACTION_UP://松开后的状态
                action_up = true;
                StringBuilder sb = new StringBuilder();
                for (Point p:points_hovered){
                    sb.append(String.valueOf(p.getNum()));
                }
                input = sb.toString();
                inputFinshListrner.doAfterinput();
                points_hovered.clear();
                break;
        }

        //重绘,重新调用onDraw方法
        postInvalidate();

        return true;
    }

    public void setInputFinshListrner(InputFinshListrner inputFinshListrner){
        this.inputFinshListrner = inputFinshListrner;
    }

    private void addToHovered(Point p){
        if (p!=null){
            for (Point pp:points_hovered){
                if (pp.getNum()==p.getNum()){
                    return ;
                }
            }
            points_hovered.add(p);
        }
    }

    //获得手指离开后最后的输入结果
    public String getInput(){
        if (input==null){
            return "null";
        }else {
            return input;
        }
    }

}
